package com.example.a10928.weifriend.ui.personal_center

import android.graphics.Color
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.support.v4.content.ContextCompat
import android.text.TextUtils
import android.view.View
import com.example.a10928.weifriend.App

import com.example.a10928.weifriend.R
import com.example.a10928.weifriend.ui.BaseActivity
import com.example.a10928.weifriend.util.L
import com.example.a10928.weifriend.util.RxUtil
import com.example.a10928.weifriend.util.StatusBarUtil
import com.githang.statusbar.StatusBarCompat
import io.reactivex.functions.Consumer
import kotlinx.android.synthetic.main.activity_bind_pay.*

class BindPayActivity : BaseActivity() {

    var flag:Boolean=false
    var isEdit:Boolean=false

    override fun getContentView(): Int {
        return R.layout.activity_bind_pay
    }

    override fun initView() {
        StatusBarCompat.setStatusBarColor(this, Color.WHITE,true)

        supportActionBar?.hide()

        tv_confirm.setOnClickListener {


            if(TextUtils.isEmpty(et_input.text.toString())){

                L.t("请输入支付宝账号")
                return@setOnClickListener
            }

            if(TextUtils.isEmpty(et_name.text.toString())){

                L.t("请输入真实姓名")
                return@setOnClickListener
            }

            if(isEdit){
                App.app().bpService.updateAlipayAccount(App.app().user.userId, et_input.text.toString(),et_name.text.toString())
                        .compose(RxUtil.io2mainLoad(this))
                        .subscribe(
                                Consumer {
                                    L.t("修改成功")

                                    Handler().postDelayed(Runnable {
                                        finish()
                                    },500)
                                },
                                Consumer {
                                    RxUtil.errorException(it)
                                }

                        )
            }else{
                App.app().bpService.bindPayAccount(App.app().user.userId, et_input.text.toString(),et_name.text.toString())
                        .compose(RxUtil.io2mainLoad(this))
                        .subscribe(
                                Consumer {
                                    L.t("绑定成功")

                                    Handler().postDelayed(Runnable {
                                        finish()
                                    },500)
                                },
                                Consumer {
                                    RxUtil.errorException(it)
                                }

                        )
            }


        }

        App.app().bpService.getUserPayAccounts(App.app().user.userId)
                .compose(RxUtil.io2mainLoad(this))
                .subscribe(
                        Consumer {
                            if(it.data!=null){
                                if(it.data.record!=null)
                                if(!TextUtils.isEmpty(it.data.record.pay_account) ){
                                    rl_bind.visibility= View.GONE
                                    rl_show.visibility=View.VISIBLE
                                    tv_account.text=it.data.record.pay_account

                                }
                            }
                        },
                        Consumer {

                        }
                )
        tv_edit.setOnClickListener {
            rl_bind.visibility= View.VISIBLE
            rl_show.visibility=View.GONE
            isEdit=true

            title_bind.setTitle("修改支付宝")
        }



    }

    override fun initData() {
    }


}
